package request;

import core.Constants;
import core.GameBoard;
import core.GameBoardTile;
import core.GameCharacter;
import core.GameMode;
import core.GameQuestion;
import core.GameUser;

import java.io.OutputStream;
import java.util.Arrays;

import response.ResponseBoardAnswer;
//import response.ResponseBoardDice;
//import response.ResponseBoardEndTurn;
//import response.ResponseBoardLocation;
//import response.ResponseBoardMove;
//import response.ResponseBoardPlayerTurn;
//import response.ResponseBoardWinner;
import response.ResponseQuestionRatio;

/**
 * @author Gary
 */
public class RequestBoardAnswer extends GameRequest {

    // User Information
    private GameUser user;
    private GameCharacter character;
    // Request Information
    private GameBoard gameBoard;
    private GameQuestion question;
    private GameCharacter nextPlayer;
    private short question_id;
    private short option;
    private boolean isCorrect;
    private String lastLocation;
    private boolean isValid;
    private boolean victory;
    private String pathList;

    public RequestBoardAnswer() {
        code = Constants.CMSG_BOARD_ANSWER;
    }

    public void parse() {
    // Protocol :
    // (short) question_id, (short) option
        user = client.getUser();
        character = user.getCharacter();
        question_id = client.readShort(dataIn);
        option = client.readShort(dataIn);
        character.setGameMode(2);
        GameMode mode = gameServer.getGameMode(character.getGameMode());        
        gameBoard = RequestBoardQuestion.gameBoard;    
        question = gameBoard.getQuestion();

        if(question!=null) {
            if (question_id == question.getID()) { 
                if (option == question.getAnswer()) {                    
                    isCorrect = true;
                } else {       
                    isCorrect = false;
                }
                /** Simplified version of adding question to log, eschewing game mode
                 * properties (see commented out code below).
                 */
                gameServer.getGameDB().createQuestionLog(user.getID(), character.getID(), question.getID(), option, isCorrect);
            } else {
                System.out.println("server side question id:"+question.getID());
            }           
        } else {
            System.out.println("server side question is null");
        }

        /**man-
        if (character.getGameMode() != 0) {
            GameMode mode = gameServer.getGameMode(character.getGameMode());

            if (mode != null) {
                if (mode.getModeType() == Constants.GAME_MODE_BOARD) {
                    gameBoard = (GameBoard) mode;

                    if (gameBoard.getMode() == Constants.BOARD_MODE_REGULAR) {
                        if (character.getID() == gameBoard.getCurrentPlayer().getID()) {
                            question = gameBoard.getQuestion();

                            if (question != null) {
                                if (question_id == question.getID()) {
                                    if (isValid = question.getOptionStatus(option)) {
                                        question.setOptionStatus(option, false);

                                        gameBoard.removeQuestionTimer();

                                        if (option == question.getAnswer()) {
                                            isCorrect = true;

                                            character.setCorrectRatio((short) (character.getCorrectRatio() + 1));

                                            victory = Arrays.equals(gameBoard.getPlayerLocation(character.getID()), gameBoard.getEnd());

                                            if (victory) {
                                                gameBoard.setWinner(character.getID());
                                                gameServer.getGameDB().setBoardGameWinner(gameBoard.getID(), user.getID(), character.getID());
                                            }
                                        } else {
                                            character.setWrongRatio((short) (character.getWrongRatio() + 1));
                                        }

                                        gameServer.getGameDB().createQuestionLog(user.getID(), character.getID(), question.getID(), option, isCorrect);

                                        //nextPlayer = gameBoard.getNextPlayer();
                                    }
                                }
                            }
                        }
                    } else if (gameBoard.getMode() == Constants.BOARD_MODE_RACE) {
                        question = gameBoard.getQuestion();

                        if (question != null) {
                            if (question_id == question.getID()) {
                                if (isValid = question.getOptionStatus(option)) {
                                    question.setOptionStatus(option, false);

                                    if (option == question.getAnswer()) {
                                        gameBoard.removeQuestionTimer();

                                        pathList = "";
                                        for (Short[] location : gameBoard.getPath(character.getID())) {
                                            pathList += location[0] + "," + location[1] + ";";
                                        }

                                        isCorrect = true;

                                        character.setCorrectRatio((short) (character.getCorrectRatio() + 1));

                                        victory = Arrays.equals(gameBoard.getPlayerLocation(character.getID()), gameBoard.getEnd());

                                        if (victory) {
                                            gameBoard.setWinner(character.getID());
                                            gameServer.getGameDB().setBoardGameWinner(gameBoard.getID(), user.getID(), character.getID());
                                        }
                                    } else {
                                        character.setWrongRatio((short) (character.getWrongRatio() + 1));
                                    }

                                    gameServer.getGameDB().createQuestionLog(user.getID(), character.getID(), question.getID(), option, isCorrect);
                                }
                            }
                        }
                    }
                }
            }
        }*/
    }

    public void respond(OutputStream output) {
        isValid=true;
        if (isValid) {
            ResponseBoardAnswer response = new ResponseBoardAnswer();
            response.set(gameServer);
            response.setOut(output);
            response.setCharacter(character);
            response.setOption(option);    
            response.setStatus(isCorrect);
            response.run();

            gameServer.updateOthersQueue(response, gameServer.getActiveModeUserList(gameBoard.getID()), user.getID());
            /**man-
            ResponseQuestionRatio ratioResponse = new ResponseQuestionRatio();
            ratioResponse.setOut(output);
            ratioResponse.setRatio(character.getQuestionRatio());
            ratioResponse.run();*/

            /*man-
            if (!victory) {
                if (gameBoard.getMode() == Constants.BOARD_MODE_REGULAR) {
                    ResponseBoardPlayerTurn playerResponse = new ResponseBoardPlayerTurn();
                    playerResponse.set(gameServer);
                    playerResponse.setOut(output);
                    playerResponse.setCharacter(nextPlayer);
                    playerResponse.run();

                    gameServer.updateOthersQueue(playerResponse, gameServer.getActiveModeUserList(gameBoard.getID()), user.getID());

                    GameBoardTile tile = gameBoard.getTile(gameBoard.getPlayerLocation(character.getID()));

                    if (isCorrect) {
                        int experience = tile.getReward() * 2;
                        gameServer.setExperienceDrop(character.getID(), experience);

                        if (tile.getType() == Constants.BOARD_BONUS) {
                            gameServer.setExperienceDrop(character.getID(), experience * (tile.getValue() - 1));
                        } else if (tile.getType() == Constants.BOARD_GOLD) {
                            gameServer.setMoneyDrop(character.getID(), tile.getValue());
                        }
                    } else {
                        Short[] location;

                        if (tile.getType() == Constants.BOARD_BLACKHOLE) {
                            location = gameBoard.setStartLocation(character.getID());
                        } else {
                            location = gameBoard.setLastLocation(character.getID());
                        }

                        lastLocation = location[0] + "," + location[1];

                        ResponseBoardLocation locationResponse = new ResponseBoardLocation();
                        locationResponse.set(gameServer);
                        locationResponse.setOut(output);
                        locationResponse.setCharacter(character);
                        locationResponse.setLocation(lastLocation);
                        locationResponse.run();
                    }
                } else if (gameBoard.getMode() == Constants.BOARD_MODE_RACE) {
                    if (isCorrect) {
                        ResponseBoardMove moveResponse = new ResponseBoardMove();
                        moveResponse.set(gameServer);
                        moveResponse.setOut(output);
                        moveResponse.setCharacter(character);
                        moveResponse.setPath(pathList);
                        moveResponse.run();
                    } else {
                        int numUsedOptions = question.getOptions().size() - question.getAvailableOptions().size();

                        if (numUsedOptions == gameBoard.getPlayerList().size()) {
                            gameBoard.removeQuestionTimer();

                            ResponseBoardEndTurn endResponse = new ResponseBoardEndTurn();
                            endResponse.setOut(output);
                            endResponse.setCharacter(character);
                            endResponse.run();

                            gameServer.updateOthersQueue(endResponse, gameServer.getActiveModeUserList(gameBoard.getID()), user.getID());

                            short result = gameBoard.useDice();

                            ResponseBoardDice diceResponse = new ResponseBoardDice();
                            diceResponse.set(gameServer);
                            diceResponse.setOut(output);
                            diceResponse.setMode(gameBoard.getMode());
                            diceResponse.setResult(result);
                            diceResponse.run();

                            gameServer.updateOthersQueue(diceResponse, gameServer.getActiveModeUserList(gameBoard.getID()), user.getID());
                        }
                    }
                }
            } else {
                if (gameBoard.getMode() == Constants.BOARD_MODE_REGULAR) {
                    ResponseBoardWinner winnerResponse = new ResponseBoardWinner();
                    winnerResponse.setOut(output);
                    winnerResponse.setCharacter(character);
                    winnerResponse.run();

                    gameServer.updateOthersQueue(winnerResponse, gameServer.getActiveModeUserList(gameBoard.getID()), user.getID());

                    int money = 5 * (gameBoard.getLevel() + 5) * 10 * gameBoard.getSize();
                    gameServer.setMoneyDrop(character.getID(), money);

                    int experience = gameServer.getGameDB().getBugExperience(gameBoard.getLevel() + 4) * gameBoard.getSize();
                    gameServer.setExperienceDrop(character.getID(), experience);
                } else if (gameBoard.getMode() == Constants.BOARD_MODE_RACE) {
                    ResponseBoardMove moveResponse = new ResponseBoardMove();
                    moveResponse.set(gameServer);
                    moveResponse.setOut(output);
                    moveResponse.setCharacter(character);
                    moveResponse.setPath(pathList);
                    moveResponse.run();
                }
            }*/
        }
    }
}

